package net.zf.dynamic.threadpool.task;

import net.zf.dynamic.threadpool.bean.DynamicThreadPoolContext;
import net.zf.dynamic.threadpool.bean.ThreadPoolConfigEntity;
import net.zf.dynamic.threadpool.redis.RedisRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 线程池数据上报
 */
public class ThreadPoolDataReportTask {

    private static final Logger log = LoggerFactory.getLogger(ThreadPoolDataReportTask.class);

    private RedisRegistry redisRegistry;

    private DynamicThreadPoolContext dynamicThreadPoolContext;

    public ThreadPoolDataReportTask(RedisRegistry redisRegistry, DynamicThreadPoolContext dynamicThreadPoolContext) {
        this.redisRegistry = redisRegistry;
        this.dynamicThreadPoolContext = dynamicThreadPoolContext;
    }

    /**
     * 定时上报线程池
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void reportThreadPoolList() {

        List<ThreadPoolConfigEntity> threadPoolConfigList = dynamicThreadPoolContext.queryThreadPoolList();
        redisRegistry.reportAllThreadPoolConfig(threadPoolConfigList);
        log.info("上报线程池配置列表：{}", threadPoolConfigList);

        for(ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigList) {
            redisRegistry.reportThreadPoolConfig(threadPoolConfigEntity);
            log.info("上报线程池核心配置：{}", threadPoolConfigEntity);
        }
    }

}
